// pages/accidentReport/reportEdit/index.js
import urls from '../../../requestUrl'
import request from '../../../baseRequest'

const { $Message } = require('../../../components/base/index.js')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        insuranceInfoComponent: {},
        accidentInfoComponent: {},
        uploaderInfo: {
            title: '事故现场照片',
            files: [],
            count: 10,
            uploadUrl: urls.baseUrl + urls.uploadUrl.commons,
            isUpload: false,
            sourceType: 'camera', //'all/camera'
            attentionText: '1.请按要求上传事故现场的真实图片\n2.上传不少于1张图片,图片必须拍照上传',
            uploadComplete: 'uploadCompleteListence'
        },
        accidentDescription: '',
        accidentImgs: [],
        showSpin: false
    },

    /**获取事故详情信息 */
    getAccidentDetailInfo() {
        let url = `${urls.accident.accidentDetail}/${this.data.accidentId}`
        request.getRequest(url, {}, (res) => {
            this.setData({
                insuranceInfoComponent: res.insuranceInfo,
                accidentInfoComponent: res.accidentInfo,
                accidentImgs: res.accidentImgs,
                accidentDescription: res.accidentDescription
            })
        }, () => {})

        // let res = testData.accidentDetailInfo
        // this.setData({
        //   insuranceInfoComponent: res.insuranceInfo,
        //   accidentInfoComponent: res.accidentInfo,
        //   accidentImgs: res.accidentImgs,
        //   accidentDescription: res.accidentDescription
        // })
    },
    /**处理修改事故信息请求 */
    handelEditAccidentOrder() {
        let param = {
            accidentId: this.data.accidentId,
            accidentInfo: this.data.formData
        }

        request.postRequest(urls.accident.accidentEdit, param, (res) => {
                this.setData({
                    showSpin: false
                })
                wx.showToast({
                    title: '修改成功',
                    complete: () => {
                        wx.redirectTo({
                            url: `/pages/accidentReport/reportDetail/index?id=${this.data.accidentId}`,
                        })
                    }
                })
            }, () => {})
            // setTimeout(() => {
            //   this.setData({
            //     showSpin: false
            //   })
            // }, 1000)

    },

    /**表单校验 */
    checkForForm(data) {
        if (data.accidentImgs.length === 0) {
            this.showErrorMessage('请上传事故现场照片')
            return false
        } else if (data.accidentImgs.length < 1) {
            this.showErrorMessage('请上传事故现场照片不少于1张')
            return false
        }
        if (!data.accidentDescription) {
            this.showErrorMessage('请填写事故现场描述')
            return false
        }
        return true
    },
    /**显示错误提示 */
    showErrorMessage(str) {
        $Message({
            content: str,
            type: 'error'
        });
    },
    /**监听图片上传完成情况*/
    uploadCompleteListence(e) {
        let detail = e.detail
        if (detail.length === this.data.formData.accidentImgs.length) {
            this.data.formData.accidentImgs = detail
            console.log(this.data.formData)
            this.handelEditAccidentOrder()
        } else {
            this.setData({
                showSpin: false
            })
            this.showErrorMessage('图片上传有误')
        }
    },
    /**处理表单提交事件 */
    handleFormSubmit(e) {
        if (this.checkForForm(e.detail.value)) {
            this.data.formData = e.detail.value
            this.setData({
                    showSpin: true,
                    ['uploaderInfo.isUpload']: true
                })
                // 返回顶部
            wx.pageScrollTo({
                scrollTop: 0,
            })
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.data.accidentId = options.id || '5bfa21988c9047c69de0572607d57712'
        this.getAccidentDetailInfo()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    // onShareAppMessage: function() {

    // }
})